package edu.mse.camel.client.uml.collab.actions;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.treeeditpart.DiagramTreeEditPart;
import com.cea.papyrus.diagraminterchange2.di2.DiConstants;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;

import edu.mse.camel.client.session.team.TEAMMEMBER_STATUS;
import edu.mse.camel.client.session.team.TeamList;
import edu.mse.camel.client.uml.collab.CollabPlugin;
import edu.mse.camel.client.uml.collab.editor.CollabActivityDiagramEditor;
import edu.mse.camel.client.uml.collab.editor.CollabClassDiagramEditor;
import edu.mse.camel.client.uml.collab.editor.CollabEditorInput;
import edu.mse.camel.client.uml.collab.editor.CollabSequenceDiagramEditor;
import edu.mse.camel.client.uml.collab.editor.CollabStateMachineDiagramEditor;
import edu.mse.camel.client.uml.collab.editor.CollabUseCaseDiagramEditor;

/**
 * @author darpan
 *
 */
public class OpenDiagramInCollabEditorAction implements IViewActionDelegate, IEditorActionDelegate {

	protected ModelManager modelManager = null;
	protected DiagramTreeEditPart diagramTreeEditPart = null;
	protected IWorkbenchPage page = null;
	
	@Override
	public void init(IViewPart view) {
		page = view.getSite().getWorkbenchWindow().getActivePage();
	}

	@Override
	public void run(IAction action) {
		if (TeamList.getInstance().getMyStatus().equals(TEAMMEMBER_STATUS.OFFLINE)) {
			ErrorDialog ed = new ErrorDialog(new Shell(),
					"Error",
					"Please Login first", new Status(IStatus.ERROR,
							CollabPlugin.PLUGIN_ID,
							"Please Login first"), IStatus.ERROR);
			ed.open();
			return;
		}
		if (TeamList.getInstance().getMyStatus().equals(TEAMMEMBER_STATUS.ONLINE)) {
			ErrorDialog ed = new ErrorDialog(new Shell(),
					"Error",
					"Please join a session first", new Status(IStatus.ERROR,
							CollabPlugin.PLUGIN_ID,
							"Please join a session first"), IStatus.ERROR);
			ed.open();
			return;
		}
		if (modelManager != null && diagramTreeEditPart != null) {
			Diagram diagram = (Diagram) diagramTreeEditPart.getModel();
			String id = getIDForEditorToCreate(diagram);
			try {
				OpenDiagramInCollaborationEditorHelper.openEditor(
						page,
						new CollabEditorInput(diagram, modelManager, true), 
						id);
			} catch (PartInitException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			Object o = ((IStructuredSelection) selection).getFirstElement();
			if (o instanceof DiagramTreeEditPart) {
				diagramTreeEditPart = (DiagramTreeEditPart) o;
				Diagram d = diagramTreeEditPart.getDiagram();
				if (isSupported(d)) {
					modelManager = diagramTreeEditPart.getModelManager();
					action.setEnabled(true);
				} else {
					action.setEnabled(false);
				}
			}
		} else {
			action.setEnabled(false);
		}
	}

	private boolean isSupported(Diagram d) {
		if (d.getName().equals(DiConstants.DEFAULT_DIAGRAM) || 
				d.getType().equals(DiConstants.CLASS_DIAGRAM) ||
				d.getType().equals(DiConstants.SEQUENCE_DIAGRAM) ||
				d.getType().equals(DiConstants.USE_CASE_DIAGRAM) ||
				d.getType().equals(DiConstants.STATE_MACHINE_DIAGRAM) ||
				d.getType().equals(DiConstants.ACTIVITY_DIAGRAM)) {
			return true;
		}
		return false;
	}

	@Override
	public void setActiveEditor(IAction action, IEditorPart targetEditor) {
		// TODO Auto-generated method stub
		
	}
	
	public String getIDForEditorToCreate(Diagram diagram) {
		if (diagram.getType().equals(DiConstants.CLASS_DIAGRAM)
				|| diagram.getName().equals(DiConstants.DEFAULT_DIAGRAM)) {
			return CollabClassDiagramEditor.ID;
		}
		if (diagram.getType().equals(DiConstants.SEQUENCE_DIAGRAM)) {
			return CollabSequenceDiagramEditor.ID;
		}
		if (diagram.getType().equals(DiConstants.USE_CASE_DIAGRAM)) {
			return CollabUseCaseDiagramEditor.ID;
		}
		if (diagram.getType().equals(DiConstants.STATE_MACHINE_DIAGRAM)) {
			return CollabStateMachineDiagramEditor.ID;
		}
		if (diagram.getType().equals(DiConstants.ACTIVITY_DIAGRAM)) {
			return CollabActivityDiagramEditor.ID;
		}
		return null;
	}
	
}
